package co.edu.unal.satacredito.mediation;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import co.edu.unal.satacredito.database.administration.ejb.BrandDaoEJBLocal;
import co.edu.unal.satacredito.database.administration.ejb.EstablishmentDaoEJBLocal;
import co.edu.unal.satacredito.database.administration.ejb.FileDaoEJBLocal;
import co.edu.unal.satacredito.database.administration.ejb.NotSyntaxTransactionDaoEJBLocal;
import co.edu.unal.satacredito.database.administration.ejb.TransactionDaoEJBLocal;
import co.edu.unal.satacredito.database.administration.exceptions.DAOException;
import co.edu.unal.satacredito.database.entity.NotSyntaxTransaction;
import co.edu.unal.satacredito.database.entity.Transaction;
import co.edu.unal.satacredito.infrastructure.dto.InvalidTransactionDTO;

/**
 * Session Bean implementation class InvalidTransactionsEJB
 */
@Stateless
public class InvalidTransactionsEJB implements InvalidTransactionsEJBRemote,
		InvalidTransactionsEJBLocal {

	private static final String NOT_BRAND_EXIST = "La marca asociada no existe";
	private static final String NOT_ESTABLISHMENT_EXIST = "El establecimiento asociado no existe";
	private static final String NOT_BRAND = "No hay marca asociada";
	private static final String NOT_ESTABLISHMENT = "No hay establecimiento asociado";
	private static final String NOT_FILE = "No existe un archivo asociado a la transacci�n";

	@EJB
	private TransactionDaoEJBLocal transactionDaoEJB;

	@EJB
	private NotSyntaxTransactionDaoEJBLocal notSyntaxDaoEJB;

	@EJB
	private BrandDaoEJBLocal brandDaoEJBLocal;

	@EJB
	private EstablishmentDaoEJBLocal establishmentDaoEJBLocal;

	@EJB
	private FileDaoEJBLocal fileDaoEJBLocal;

	/**
	 * Default constructor.
	 */
	public InvalidTransactionsEJB() {
	}

	private String getEstablishmentId(String fileId) {
		try {
			return fileDaoEJBLocal.findFileById(fileId).getEstablishment() + "";
		} catch (Exception e) {
			return NOT_FILE;
		}
	}

	private String getBrandId(String fileId) {
		try {
			return fileDaoEJBLocal.findFileById(fileId).getBrand() + "";
		} catch (Exception e) {
			return NOT_FILE;
		}
	}

	private String getNameBrand(String brandId) {
		try {
			return brandDaoEJBLocal.findBrandById(brandId).getBrandName();
		} catch (DAOException e) {
			return NOT_BRAND;
		} catch (NullPointerException e) {
			return NOT_BRAND_EXIST;
		}
	}

	private String getNameEstablishment(String brandId) {
		try {
			return establishmentDaoEJBLocal.findEstablishmentById(brandId)
					.getNameEstablishment();
		} catch (DAOException e) {
			return NOT_ESTABLISHMENT;
		} catch (NullPointerException e) {
			return NOT_ESTABLISHMENT_EXIST;
		}
	}

	private List<NotSyntaxTransaction> getInvBySyn(Date initDate, Date endDate) {
		try {
			return notSyntaxDaoEJB.findNotSyntaxByDate(initDate, endDate);
		} catch (DAOException e) {
			return new ArrayList<NotSyntaxTransaction>();
		}
	}

	private List<Transaction> getInvByIde(Date initDate, Date endDate) {
		try {
			return transactionDaoEJB.findTransactionIdentificationByDate(
					initDate, endDate);
		} catch (DAOException e) {
			return new ArrayList<Transaction>();
		}
	}

	private List<Transaction> getInvByCard(Date initDate, Date endDate) {
		try {
			return transactionDaoEJB.findTransactionCardByDate(initDate,
					endDate);
		} catch (DAOException e) {
			return new ArrayList<Transaction>();
		}
	}

	@Override
	public List<InvalidTransactionDTO> getNotSyntax(Date initDate, Date endDate) {
		InvalidTransactionDTO e = new InvalidTransactionDTO();
		e.setIdFile("" + Math.random());
		List<NotSyntaxTransaction> list = getInvBySyn(initDate, endDate);
		for (NotSyntaxTransaction notSyntaxTransaction : list) {
			System.out.println(notSyntaxTransaction.getValidationDate());
		}
		List<InvalidTransactionDTO> finallist = new ArrayList<InvalidTransactionDTO>();
		// finallist.add(e);
		for (NotSyntaxTransaction t : list) {
			InvalidTransactionDTO temp = new InvalidTransactionDTO();
			temp.setIdFile("" + t.getFileId());
			temp.setDate(t.getValidationDate().toString());
			temp.setBrand(getNameBrand(t.getBrand() + ""));
			temp.setEstablishment(getNameEstablishment(""
					+ t.getEstablishment()));
			temp.setNumberLine("" + t.getTransactionLine());
			temp.setFranshise(" ");
			finallist.add(temp);
		}
		System.out.println(finallist.size());
		for (InvalidTransactionDTO invalidTransactionDTO : finallist) {
			System.out.println(invalidTransactionDTO.getDate());
		}
		return finallist;
	}

	@Override
	public List<InvalidTransactionDTO> getNotIdentification(Date initDate,
			Date endDate) {
		List<Transaction> list = getInvByIde(initDate, endDate);
		List<InvalidTransactionDTO> finallist = TransactionToInvalidTransaction(list);
		return finallist;
	}

	@Override
	public List<InvalidTransactionDTO> getNotCard(Date initDate, Date endDate) {
		List<Transaction> list = getInvByCard(initDate, endDate);
		List<InvalidTransactionDTO> finallist = TransactionToInvalidTransaction(list);
		return finallist;
	}

	private List<InvalidTransactionDTO> TransactionToInvalidTransaction(
			List<Transaction> list) {
		List<InvalidTransactionDTO> finallist = new ArrayList<InvalidTransactionDTO>();
		for (Transaction t : list) {
			InvalidTransactionDTO temp = new InvalidTransactionDTO();
			temp.setIdFile("" + t.getFileId());
			temp.setDate(t.getValidationDate() + "");
			temp.setBrand(getNameBrand(getBrandId(t.getFileId() + "")));
			temp.setEstablishment(getNameEstablishment(getEstablishmentId(t
					.getFileId() + "")));
			temp.setNumberLine("" + t.getFileNumber());
			temp.setFranshise(" ");
			finallist.add(temp);
		}
		return finallist;
	}

	@Override
	public List<InvalidTransactionDTO> getNotSyntaxReal() {
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = (Calendar) finalDate.clone();
		finalDate.set(Calendar.MILLISECOND, 0);
		initDate.set(Calendar.MILLISECOND, 0);
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 1);
		System.out.println(initDate.getTime());
		System.out.println(finalDate.getTime());
		return getNotSyntax(initDate.getTime(), finalDate.getTime());
	}

	@Override
	public List<InvalidTransactionDTO> getNotIdenReal() {
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = (Calendar) finalDate.clone();
		finalDate.set(Calendar.MILLISECOND, 0);
		initDate.set(Calendar.MILLISECOND, 0);
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 1);
		System.out.println(initDate.getTime());
		System.out.println(finalDate.getTime());
		return getNotIdentification(initDate.getTime(), finalDate.getTime());
	}

	@Override
	public List<InvalidTransactionDTO> getNotCardReal() {
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = (Calendar) finalDate.clone();
		finalDate.set(Calendar.MILLISECOND, 0);
		initDate.set(Calendar.MILLISECOND, 0);
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 1);
		System.out.println(initDate.getTime());
		System.out.println(finalDate.getTime());
		return getNotCard(initDate.getTime(), finalDate.getTime());
	}

}
